package ntust.tw.embedded_project_v1;

import static android.provider.BaseColumns._ID;
import static ntust.tw.embedded_project_v1.DbConstants.AUTHOR;
import static ntust.tw.embedded_project_v1.DbConstants.BOOKNAME;
import static ntust.tw.embedded_project_v1.DbConstants.EXPERIENCE;
import static ntust.tw.embedded_project_v1.DbConstants.PIC;
import static ntust.tw.embedded_project_v1.DbConstants.PUBLISHER;
import static ntust.tw.embedded_project_v1.DbConstants.TABLE_NAME;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Activity_QueryBook extends Activity{

	private DbHelper dbhelper = null;
    private ListView listData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_querybook);
		init();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeDatabase();
    }
    private void openDatabase(){
        dbhelper = new DbHelper(this); 
    }

    private void closeDatabase(){
        dbhelper.close();
    }
	@Override
    protected void onPause(){
    	super.onPause();
    	finish();
    }
	private void init(){
		openDatabase();
		listData = (ListView)findViewById(R.id.listData);
		display();
	}
    @SuppressWarnings("deprecation")
	private Cursor getCursor(){
    	SQLiteDatabase db = dbhelper.getReadableDatabase();
    	String[] columns = {_ID, BOOKNAME, AUTHOR, PUBLISHER, PIC, EXPERIENCE};
    	
    	Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
    	startManagingCursor(cursor);
    	
    	return cursor;
    }
	public void display() {
    	final Cursor cursor = getCursor();
    	
    	String[] from = {_ID, BOOKNAME, AUTHOR, PUBLISHER, EXPERIENCE};// still have pic need to fix.
    	int[] to = {R.id.txtID, R.id.txtBookName, R.id.txtAuthor, R.id.txtPublisher, R.id.txtExp};
    	
		MySimpleCursorAdapter adapter = new MySimpleCursorAdapter(this,
				R.layout.item_data, cursor, from, to);
		listData.setAdapter(adapter);
    	listData.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> a, View v, final int position, long id) {
        		new AlertDialog.Builder(Activity_QueryBook.this)
            	.setMessage("功能表")
            	.setNegativeButton("確認", new DialogInterface.OnClickListener() {
            		public void onClick(DialogInterface arg0, int arg1) {
            		}})
            	.setPositiveButton("刪除紀錄", new DialogInterface.OnClickListener() {
            		public void onClick(DialogInterface arg0, int arg1) {
            			cursor.moveToPosition(position);
            			del(cursor.getString(0));
            			display();
            		}
            	}).show();
        		}
        	});

	}

    public void confirmBtn(View v){
    	finish();
    }
    public void backBtn(View v){
    	finish();
    }
    private void del(String id){
    	SQLiteDatabase db = dbhelper.getWritableDatabase();
    	db.delete(TABLE_NAME, _ID + "=" + id, null);
    }
}
